﻿using Ext.Net;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CRM_Store_ERP_DeliveryManager : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState_OrderSre = "InDate desc";
            AddHandlersToControls();
            BindCenterStore(Store_Rome, Request.QueryString["tabid"]);
            gridPanel_listdata.Reload();
        }
    }

    private void AddHandlersToControls()
    {
        string Url = G_Url_allstring + "/CRM_Store/ERP_DeliveryEdit.aspx";

        string tabid = base.G_FrameTabid;
        //popfortab(GridName, Url, StringName, StringField, RowId, AttQryStr, TabId, TabName, flag)
        string handler = "popfortab(gridPanel_listdata, '{0}', '{1}', '{2}', {3}, '{4}', '{5}', '{6}', {7});";
        extBtn_Add.Listeners.Click.Handler = string.Format(handler, Url, "ObjId", "Add", 0, "", tabid, "退货单（详细）", 1);
        extBtn_Edit.Listeners.Click.Handler = string.Format(handler, Url, "ObjId", "BoundId", 0, "", tabid, "退货单（详细）", 2);
        gridPanel_listdata.Listeners.RowDblClick.Handler = string.Format(handler, Url, "ObjId", "BoundId", "rowIndex", "", tabid, "退货单（详细）", 3);
    }

    string _QueryString;
    private string GetQryString()
    {
        string result = " State=2 ";

        if (exttxtf_BoundNo.Text.Trim() != "")
            result += " and BoundNo like'%" + FilterSpecChar(exttxtf_BoundNo.Text.Trim()) + "%'";
        if (!string.IsNullOrEmpty(extcomb_STOREROOM_ID.SelectedItem.Text))
            result += " and StoreRoomId='" + extcomb_STOREROOM_ID.SelectedItem.Value + "' ";
        if (extdate_start.SelectedValue != null)
            result += " and CreateTime>='" + extdate_start.SelectedValue + "' ";
        if (extdate_end.SelectedValue != null)
            result += " and CreateTime<='" + extdate_end.SelectedValue + "' ";
        if (!string.IsNullOrEmpty(extcomb_CustomerId.SelectedItem.Text))
            result += " and CustomerName='" + extcomb_CustomerId.SelectedItem.Text + "' ";
        return result;
    }

    protected void MyData_Refresh(object sender, StoreRefreshDataEventArgs e)
    {
        _QueryString = GetQryString();
        int count = CRMMis.BLL.ERP_OutBound.GetRecorderCount(_QueryString);
        BindGridPSData(e, ViewState_OrderSre, PagingToolbar1, Store1, count);
    }

    public override DataTable GetPSData(int PageSize, int CurPage, string OrderStr)
    {
        ViewState_OrderSre = OrderStr;
        DataTable dt = CRMMis.BLL.ERP_OutBound.GetPageList(PageSize, CurPage, _QueryString, OrderStr).Tables[0];
        return dt;
    }

    [DirectMethod]
    public void DeleteModel(int BoundId)
    {
        CommonLib.BLL.MyTransaction myTrans = new CommonLib.BLL.MyTransaction();
        try
        {
            var model = CRMMis.BLL.ERP_OutBound.GetModel(BoundId);
            //判断是否有审核员角色
            if (BS_LoginRoles.Contains("02"))
            {
                if (model.State == 2)
                {
                    X.Msg.Alert("系统提示", "已审核的数据不能删除！").Show();
                    return;
                }
            }
            else    //普通用户
            {
                if (model.State > 0)
                {
                    X.Msg.Alert("系统提示", "已提交的数据不能删除！").Show();
                    return;
                }
            }
            myTrans.BeginTransaction();
            CRMMis.BLL.ERP_OutBoundDetail.DeleteFK(myTrans, "DBoundId = " + BoundId);
            CRMMis.BLL.ERP_OutBound.Delete(myTrans, BoundId);
            myTrans.CommitTran();
            X.Msg.Alert("系统提示", "删除成功！").Show();
            gridPanel_listdata.Reload();

        }
        catch (Exception ex)
        {
            myTrans.RollBackTran();
            X.Msg.Alert("系统提示", "删除失败！</br>" + ex.Message).Show();
        }
    }

    #region 供货商选择

    protected void StoreCustomer_Refresh(object sender, StoreRefreshDataEventArgs e)
    {
        var qStr = GetCustomerQryString();
        int recordcount = CRMMis.BLL.ERP_Customer.GetRecorderCount(qStr);
        int PageSize = extcomb_CustomerId.PageSize;
        int CurPage = e.Start / PageSize + 1;
        e.Total = recordcount;
        DataTable suppData = CRMMis.BLL.ERP_Customer.GetPageList(PageSize, CurPage, qStr, "CustomerName,Spell,Address").Tables[0];
        Store_Customer.DataSource = suppData;
        Store_Customer.DataBind();
    }

    private string GetCustomerQryString()
    {
        string StrSql = "DelState=0 ";
        string selText = FilterSpecChar(extcomb_CustomerId.SelectedItem.Text.Trim());
        if (!string.IsNullOrEmpty(selText))
            StrSql += " and ( CustomerName like '%" + selText + "%' or Spell like '%" + selText + "%') ";
        return StrSql;
    }

    #endregion

}